<?php

namespace App\Http\Controllers\mobile;

use App\Models\Book;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use App\Models\Section;

class DefaultController extends BaseController {
	
	public function index() {
		$books = Book::whereNotNull('name')->orderBy('clicks', 'DESC')->take(30)->get();
		return view('mobile.index', compact('books'));
	}

	public function search() {
		$keywords = Input::get('keywords');
		$books = Book::where('name', 'like', '%'.$keywords.'%')
				->orderBy('clicks', 'DESC')
				->get();
		$likeBooks = Book::orderBy('likes', 'DESC')
				->take(4)
				->get();
		return view('mobile.search', compact('keywords', 'books', 'likeBooks'));
	}
	
	public function history() {
		$bookIds = Session::all();
		$books = [];
		foreach ($bookIds as $key => $value) {
			$book = Book::find($key);
			if(!is_null($book)) {
				$section = Section::where('order', (int)$value)->first();
				$books[] = [
					'book' => $book,
					'section' => $section
				];
			}
		}
		return view('mobile.history', compact('books'));
	}
}
